<?php
/**
* Menu desplegable basado en Jquery
*
* 
* @package Zend XML Admin
* @version $Id:$
* @since 0.1
*/

class Zwei_Utils_Menu
{
	private $_out;
	
	/**
	 * 
	 * @param Array() $tree
	 */
    function __construct($tree)
    {
    	$this->_out="
    	    <script type=\"text/javascript\"> 
    	    $(document).ready(function(){
                $('ul.subnav').parent().append('<span></span>'); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
                $('ul.topnav li span').click(function() { //When trigger is clicked...
                
                //Following events are applied to the subnav itself (moving subnav up and down)
                $(this).parent().find('ul.subnav').slideDown('fast').show(); //Drop down the subnav on click
        
                $(this).parent().hover(function() {
                }, function(){  
                    $(this).parent().find('ul.subnav').slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
                });
        
                //Following events are applied to the trigger (Hover events for the trigger)
                }).hover(function() { 
                    $(this).addClass('subhover'); //On hover over, add class 'subhover'
                }, function(){  //On Hover Out
                    $(this).removeClass('subhover'); //On hover out, remove class 'subhover'
            });
        
        });
        </script>
    	";
    	$this->_out.='<ul class="topnav">';
    	foreach($tree as $t){
    		if (isset($t['url'])){
    			$this->_out.='<li><a href="'.BASE_URL.$t['url'].'">'.$t['label'].'</a>';
    		} else {
    			$this->_out.='<li><div class="no_linked">'.$t['label'].'</div>';
    		} 

    		
    		if(isset($t['children'])){
    			$this->_out.='<ul class="subnav">';
	    		foreach($t['children'] as $t2){
	    			$this->_out.='<li><a href="'.BASE_URL.@$t2['url'].'">'.$t2['label'].'</a>';
	    		}
	    		$this->_out.='</ul>';
    		}
    		$this->_out.='</li>';
    		
    	}
    	
    	$this->_out.='</ul>';
    }
    
    /**
     * 
     * @return string html
     */
    
    public function display(){
    	return $this->_out;
    }
}
